package com.coke.demo.controller;

import com.coke.demo.model.Laboratory;
import com.coke.demo.model.Result;
import com.coke.demo.service.LabService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 19602
 */
@Controller
@RequestMapping("lab")
public class LabController {

    private LabService labService;
    private RedisTemplate redisTemplate;

    @Autowired
    public void setLabService(LabService labService) {
        this.labService = labService;
    }

    @Autowired
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        this.redisTemplate = redisTemplate;
    }

    /**
     * 查询所有实验室信息
     *
     * @return List<Laboratory>
     */
    @ResponseBody
    @PostMapping("/findAllLabs")
    @CrossOrigin
    public Result findAllLabs() {
        List<Laboratory> laboratories = labService.findAllLabs();
        if (laboratories != null) {
            return new Result("200", "查询成功", laboratories);
        } else {
            return new Result("500", "查询失败");
        }
    }

    /**
     * 删除实验室信息
     *
     * @param id:实验室id
     * @return Result
     */
    @ResponseBody
    @DeleteMapping("/deleteLabById")
    @CrossOrigin
    public Result deleteLabById(Integer id) {
        if (labService.deleteLabById(id)) {
            return new Result("200", "删除成功");
        } else {
            return new Result("500", "删除失败");
        }
    }

    /**
     * 删除多个实验室信息
     *
     * @param ids:多个实验室id
     * @return Result
     * @throws Exception:异常
     */
    @ResponseBody
    @DeleteMapping("/deleteLabSome")
    @CrossOrigin
    public Result deleteLabSome(Integer[] ids) throws Exception {
        for (Integer id : ids) {
            labService.deleteLabById(id);
        }
        return new Result("200", "删除成功");
    }

    /**
     * 根据id查询实验室信息
     *
     * @param labid 实验室id
     * @return Result
     */
    @ResponseBody
    @PostMapping("/findLabById")
    @CrossOrigin
    public Result findLabById(Integer labid) {
        Laboratory laboratory = labService.findLabById(labid);
        if (laboratory != null) {
            return new Result("200", "请求成功", laboratory);
        } else {
            return new Result("500", "请求失败");
        }
    }

    /**
     * 根据名称模糊查询实验室信息
     *
     * @param labname 实验室名称
     * @return Result
     */
    @ResponseBody
    @PostMapping("/findLabByName")
    @CrossOrigin
    public Result findLabByName(String labname) {
        ArrayList<Laboratory> labs = labService.findLabByName(labname);
        if (labs != null) {
            return new Result("200", "请求成功", labs);
        } else {
            return new Result("500", "请求失败");
        }
    }

    /**
     * 根据实验室id修改实验室信息
     *
     * @param laboratory 修改后的实验室信息
     * @return Result
     */
    @ResponseBody
    @PostMapping("/updateLabById")
    @CrossOrigin
    public Result updateLabById(Laboratory laboratory) {
        if (labService.updateLabById(laboratory)) {
            return new Result("200", "修改成功");
        } else {
            return new Result("200", "修改失败");
        }
    }

    /**
     * 添加新的实验室
     *
     * @param laboratory 实验室信息
     * @return Result
     */
    @ResponseBody
    @PostMapping("/insertLab")
    @CrossOrigin
    public Result insertLab(Laboratory laboratory) {
        if (labService.insertLab(laboratory)) {
            return new Result("200", "添加成功");
        } else {
            return new Result("500", "添加失败");
        }
    }
}
